結合上一篇的SharedPreferences建立一個簡易歷史紀錄的工具
MainActivity UI
拉兩個帳號密碼的輸入跟checkbox+button 以及一個顯示歷史紀錄的textview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="帳號"
android:inputType="text" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/rememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住我" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登入" />
<TextView
android:id="@+id/loginHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>
先宣告變數跟綁定元件
private EditText usernameEditText, passwordEditText;
private CheckBox rememberMeCheckBox;
private Button loginButton;
private TextView loginHistory;
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
rememberMeCheckBox = findViewById(R.id.rememberMe);
loginButton = findViewById(R.id.loginButton);
loginHistory = findViewById(R.id.loginHistory);
設定SharedPreferences 的檔案名稱private static final String PREF_NAME = "LoginPrefs";
建立SharedPreferences 並設定檔案櫃模式SharedPreferences sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
boolean isRemembered = sharedPreferences.getBoolean("rememberMe", false);
// 如果使用者選擇了記住密碼,則自動填充用戶名和密碼
if (isRemembered) {
String savedUsername = sharedPreferences.getString("username", "");
String savedPassword = sharedPreferences.getString("password", "");
usernameEditText.setText(savedUsername);
passwordEditText.setText(savedPassword);
rememberMeCheckBox.setChecked(true);
}
// 顯示登入紀錄
String history = sharedPreferences.getString("loginHistory", "No login history");
loginHistory.setText(history);
button的點擊事件
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
boolean rememberMe = rememberMeCheckBox.isChecked();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "Please enter both username and password", Toast.LENGTH_SHORT).show();
} else {
// 保存登入狀態和記住密碼的選擇
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.putBoolean("rememberMe", rememberMe);
// 紀錄登入時間
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
String loginRecord = username + " logged in at " + currentTime + "\n";
// 更新登入歷史
String existingHistory = sharedPreferences.getString("loginHistory", "");
editor.putString("loginHistory", existingHistory + loginRecord);
editor.apply();
// 更新 UI 顯示登入紀錄
loginHistory.setText(existingHistory + loginRecord);
// 跳轉到歡迎頁面
startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
finish(); // 結束當前 Activity
}
}
});